What is pupa?
The pupa npm package is a simple string interpolation library. It allows you to replace placeholders in a string with actual values from an object or an array. It is useful for templating and customizing messages or configurations.
What are pupa's main functionalities?
String interpolation with an object
This feature allows you to interpolate values from an object into a string. For example, if you have an object with properties 'name' and 'notifications', pupa can replace the placeholders in the string with the corresponding values from the object.
"Hello, {name}! You have {notifications} new notifications."
String interpolation with an array
This feature allows you to interpolate values from an array into a string. The placeholders are indexed based on the array's indices, allowing you to insert array elements into the string at the specified positions.
"Hello, {0}! You are number {1} in line."
Nested object interpolation
Pupa also supports nested object interpolation, where you can access nested properties within an object to replace placeholders in a string.
"Hello, {user.name}! Your role is {user.role}."
Escape interpolation syntax
If you need to include the interpolation syntax as a literal part of the string, you can escape it using a backslash. This tells pupa not to interpret it as a placeholder.
"This is a message with an escaped placeholder: \{escaped\}"
Other packages similar to pupa
mustache
Mustache is a logic-less template syntax that can be used for HTML, config files, source code, etc. It is more feature-rich than pupa, supporting sections, inverted sections, and partials, making it suitable for more complex templating tasks.
handlebars
Handlebars is a superset of Mustache that adds more power and flexibility. It includes features like block expressions and helpers, which allow for more dynamic and reusable templates. It is more complex than pupa but also more capable for extensive templating needs.
ejs
EJS, or Embedded JavaScript templates, is a templating engine that allows you to generate HTML markup with plain JavaScript. It offers greater control and flexibility than pupa, with features like includes, conditional statements, and loops.
lodash.template
Lodash's template function is part of the larger Lodash utility library. It provides a simple interpolation feature similar to pupa but also includes additional template features like evaluation of arbitrary JavaScript code within templates.
pupa
Simple micro templating
Useful when all you need is to fill in some placeholders.
Install
$ npm install pupa
Usage
import pupa from 'pupa';
pupa('The mobile number of {name} is {phone.mobile}', {
name: 'Sindre',
phone: {
mobile: '609 24 363'
}
});
pupa('I like {0} and {1}', ['🦄', '🐮']);
pupa('I like {{0}} and {{1}}', ['<br>🦄</br>', '<i>🐮</i>']);
API
pupa(template, data, options?)
template
Type: string
Text with placeholders for data
properties.
data
Type: object | unknown[]
Data to interpolate into template
.
options
Type: object
ignoreMissing
Type: boolean
Default: false
By default, Pupa throws a MissingValueError
when a placeholder resolves to undefined
. With this option set to true
, it simply ignores it and leaves the placeholder as is.
transform
Type: ((data: {value: unknown; key: string}) => unknown) | undefined
(default: ({value}) => value
)
Performs arbitrary operation for each interpolation. If the returned value was undefined
, it behaves differently depending on the ignoreMissing
option. Otherwise, the returned value will be interpolated into a string (and escaped when double-braced) and embedded into the template.
MissingValueError
Exposed for instance checking.
FAQ
What about template literals?
Template literals expand on creation. This module expands the template on execution, which can be useful if either or both template and data are lazily created or user-supplied.
Related